using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Red_Gun
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game : GameFrames.Game
    {
        //public GraphicsDeviceManager graphics;
        //public SpriteBatch spriteBatch;
        //public static Game me;
        public static Game thisGame;
        public static Player hero;

        public static class Movement
        {
            public static void MoveUp()
            {
                if (Game.thisGame.canMoveUp())
                {
                    if (hero.isCenteredY())
                    {
                        Game.me.mainGrid.markToMove(0, hero.moveRate);
                    }
                    else
                    {
                        hero.moveN();
                        hero.yOffset -= hero.moveRate;
                    }
                }
                else
                {
                    Player.myArray[0].moveN();
                    hero.yOffset -= hero.moveRate;
                }
            }
            public static void MoveDown()
            {
                if (Game.thisGame.canMoveDown())
                {
                    if (hero.isCenteredY())
                    {
                        Game.thisGame.mainGrid.markToMove(0, -hero.moveRate);
                    }
                    else
                    {
                        hero.moveS();
                        hero.yOffset += hero.moveRate;
                    }
                }
                else
                {
                    Player.myArray[0].moveS();
                    hero.yOffset += hero.moveRate;
                }
            }
            public static void MoveRight()
            {
                if (Game.thisGame.canMoveRight())
                {
                    if (hero.isCenteredX())
                    {
                        Game.thisGame.mainGrid.markToMove(-hero.moveRate, 0);
                    }
                    else
                    {
                        hero.moveE();
                        hero.xOffset += hero.moveRate;
                    }
                }
                else
                {
                    Player.myArray[0].moveE();
                    hero.xOffset += hero.moveRate;
                }
            }
            public static void MoveLeft()
            {
                if (Game.thisGame.canMoveLeft())
                {
                    //move erethin'
                    if (hero.isCenteredX())
                    {
                        Game.thisGame.mainGrid.markToMove(hero.moveRate, 0);
                    }
                    else
                    {
                        hero.moveW();
                        hero.xOffset -= hero.moveRate;
                    }
                }
                else
                {
                    Player.myArray[0].moveW();
                    hero.xOffset -= hero.moveRate;
                }
            }
        }

        public bool canMoveRight()
        {
            if (mainGrid.right() > 800 + hero.moveRate)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool canMoveLeft()
        {
            if (mainGrid.left < 0 - hero.moveRate)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool canMoveUp()
        {
            if (mainGrid.top < 0 - hero.moveRate)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool canMoveDown()
        {
            if (mainGrid.bottom() > 600 + hero.moveRate)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public struct Layout
        {
            public const int FrameLength = 32;
        }
        public struct Direct
        {
            public const int Down = 0;
            public const int Left = 1;
            public const int Up = 2;
            public const int Right = 3;
            public const int DR = 4;
            public const int DL = 5;
            public const int UL = 6;
            public const int UR = 7;
        }

        public Game()
        {
            //graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            thisGame = this;
            me = this;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            int xMax = 8;
            int yMax = 4;
            string[,] allSets = new string[yMax, xMax];
            
            string folder = "Sprites/Hero/";

            allSets[Spriteset.hero, 0] = folder + "hero1";
            allSets[Spriteset.hero, 1] = folder + "hero2";
            allSets[Spriteset.hero, 2] = folder + "hero3";
            allSets[Spriteset.hero, 3] = folder + "hero4";
            allSets[Spriteset.hero, 4] = folder + "hero5";
            allSets[Spriteset.hero, 5] = folder + "hero6";
            allSets[Spriteset.hero, 6] = folder + "hero7";
            allSets[Spriteset.hero, 7] = folder + "hero8";

            folder = "Sprites/Tiles/Static/";
            allSets[Spriteset.baseTile, 0] = folder + "basePanelTile";
           // allSets[Spriteset.baseTile, 1] = folder + "tile2";
           // allSets[Spriteset.baseTile, 2] = folder + "tile3";
          //  allSets[Spriteset.baseTile, 3] = folder + "tile4";

            folder = "Sprites/Tiles/Flashy/";
            allSets[Spriteset.flashTile, 0] = folder + "tile1";
            allSets[Spriteset.flashTile, 1] = folder + "tile2";
            allSets[Spriteset.flashTile, 2] = folder + "tile3";
            allSets[Spriteset.flashTile, 3] = folder + "tile4";

            folder = "Sprites/Tiles/Lava/";
            allSets[Spriteset.lavaTile, 0] = folder + "lava1";
            allSets[Spriteset.lavaTile, 1] = folder + "lava2";
            allSets[Spriteset.lavaTile, 2] = folder + "lava3";
            allSets[Spriteset.lavaTile, 3] = folder + "lava2";

            Spriteset.setUpAllSpriteSets(allSets, xMax, yMax);

            Spriteset.myArray[Spriteset.flashTile].setUniformAnimationRate(1);
            Spriteset.myArray[Spriteset.lavaTile].setUniformAnimationRate(10);

            this.mainGrid = World001.getGrid(World001.getWorldIndex());

            hero = new Player(384, 284);
            hero.setMoveRate(3);
            
            hero.addSprites(Spriteset.myArray[Spriteset.hero]);
            
            //Bitmap[] newMaps = new Bitmap[2];
            //newMaps[0] = new Bitmap(buildTexture(path), path);
            //newMaps[1] = new Bitmap();
            
            //Spriteset.myArray[0] = new Spriteset(newMaps[]);
            
            //newMaps = new Bitmap[3];
            //newMaps[0] = new Bitmap();
            //newMaps[1] = new Bitmap();
            //newMaps[2] = new Bitmap();

            //Spriteset.myArray[1] = new Spriteset(newMaps[]);

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            this.graphics.IsFullScreen = true;
            this.graphics.ApplyChanges();
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (isEscDown())
            {
                this.Exit();
            }

            if(Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.F7))
            {
                this.graphics.IsFullScreen = true;
                this.graphics.ApplyChanges();
            }

            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.F8))
            {
                this.graphics.IsFullScreen = false;
                this.graphics.ApplyChanges();
            }

            if (isOnlyLeftDown())
            {
                Player.myArray[0].mySpriteset.indexActiveBMP = Game.Direct.Left;

                if (!isTabDown())
                {
                    Movement.MoveLeft();
                }
            }
            else if (isOnlyRightDown())
            {
                Player.myArray[0].mySpriteset.indexActiveBMP = Game.Direct.Right;

                if (!isTabDown())
                {
                    Movement.MoveRight();
                }
            }
            else if (isOnlyDownDown())
            {
                Player.myArray[0].mySpriteset.indexActiveBMP = Game.Direct.Down;

                if (!isTabDown())
                {
                    Movement.MoveDown();
                }
            }
            else if (isOnlyUpDown())
            {
                Player.myArray[0].mySpriteset.indexActiveBMP = Game.Direct.Up;
                
                if (!isTabDown())
                {
                    Movement.MoveUp();
                }
            }
            else if (isUpAndLeftDown())
            {
                Player.myArray[0].mySpriteset.indexActiveBMP = Game.Direct.UL;
                if (!isTabDown())
                {
                    Movement.MoveUp();
                    Movement.MoveLeft();
                }
            }
            else if (isUpAndRightDown())
            {
                Player.myArray[0].mySpriteset.indexActiveBMP = Game.Direct.UR;
                if (!isTabDown())
                {
                    Movement.MoveRight();
                    Movement.MoveUp();
                }
            }
            else if (isDownAndLeftDown())
            {
                Player.myArray[0].mySpriteset.indexActiveBMP = Game.Direct.DL;
                if (!isTabDown())
                {
                    Movement.MoveLeft();
                    Movement.MoveDown();
                }
            }
            else if (isDownAndRightDown())
            {
                Player.myArray[0].mySpriteset.indexActiveBMP = Game.Direct.DR;
                if (!isTabDown())
                {
                    Movement.MoveRight();
                    Movement.MoveDown();
                }
            }

            // TODO: Add your update logic here
            
            Spriteset.processAllSpritesets();
            
            this.mainGrid.doMove();
            hero.doMove();

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();

            if (mainGrid != null)
            {
                for (int j = 0; j < mainGrid.gridHeight; j++)
                {
                    for (int i = 0; i < mainGrid.gridWidth; i++)
                    {
                        if (mainGrid.myCells[j, i] != null)
                        {
                            spriteBatch.Draw(mainGrid.myCells[j, i].myImage(), mainGrid.myCells[j, i].rectangle(), mainGrid.myCells[j, i].myColor());
                        }
                    }
                }
            }

            if (Player.myArray[0] != null)
            {
                spriteBatch.Draw(Player.myArray[0].myImage(), Player.myArray[0].rectangle(), Player.myArray[0].myColor());
            }

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
